package com.example.springback.controller;

import com.example.springback.common.Result;
import com.example.springback.domain.Enterprise;
import com.example.springback.domain.Pension;
import com.example.springback.service.PensionService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/pension")
public class PensionController {
    @Resource
    private PensionService pensionService;

    //查询某企业养老金缴纳记录
    @GetMapping("/searchEnterprisePage")
    public Result selectEnterprisePage(Pension pension , @RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "5") Integer pageSize){
        PageInfo<Pension> pensionPageInfo =pensionService.selectEnterprisePage(pension,pageNumber,pageSize);
        return Result.success(pensionPageInfo);
    }
    //查询所有企业养老金缴纳记录
    @GetMapping("/searchPage")
    public Result selectPage(String enterpriseName , @RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "5") Integer pageSize){
        PageInfo<Enterprise> pensionPageInfo =pensionService.selectPage(enterpriseName,pageNumber,pageSize);
        return Result.success(pensionPageInfo);
    }
    //新增缴纳记录
    @PostMapping("/add")
    public Result add(@RequestBody Map<String, Object> requestBody){
        Enterprise enterprise = new Enterprise();
        enterprise.setEnterpriseId((Integer) requestBody.get("enterpriseId"));
        enterprise.setEnterpriseName((String) requestBody.get("enterpriseName"));
        enterprise.setTaxNumber((String) requestBody.get("taxNumber"));
        enterprise.setBossName((String) requestBody.get("bossName"));
        enterprise.setPhone((String) requestBody.get("phone"));
        Pension pension = new Pension();
        pension.setEnterpriseId((Integer) requestBody.get("enterpriseId"));
        pension.setPensionTerm((String) requestBody.get("pensionTerm"));
        pension.setTaxAuthority((String) requestBody.get("taxAuthority"));
        pensionService.add(pension,enterprise);
        return Result.success();
    }

    //批量更新,更新缴纳记录，适用于补缴
    @PostMapping("/update")
    public Result updateBatch(@RequestBody List<Pension> pensionList){
        pensionService.updateBatch(pensionList);
        return Result.success();
    }

    //根据月份获取养老保险金额，例如近三个月，近六个月……
    @GetMapping("/paymentTrend")
    public Result getPaymentTrend(@RequestParam(defaultValue = "6") int months) {
        List<Map<String, Object>> list = pensionService.getMonthlyTrend(months);
        List<String> monthsList = new ArrayList<>();
        List<BigDecimal> amounts = new ArrayList<>();

        for (Map<String, Object> entry : list) {
            monthsList.add((String) entry.get("month"));
            amounts.add((BigDecimal) entry.get("total"));
        }
        Map<String, Object> result = new HashMap<>();
        result.put("months", monthsList);
        result.put("amounts", amounts);
        return Result.success(result);
    }
    //获取每个企业的每个月缴纳的养老保险并做成柱状图
    @GetMapping("/compare")
    public Result getEnterpriseComparison(@RequestParam String month) {
        try {
            List<Map<String, Object>> list = pensionService.getMonthlyPensionByEnterprise(month);
            return Result.success(list);
        } catch (Exception e) {
            return Result.error("500", "查询失败：" + e.getMessage());
        }
    }
}



